<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function getAddrBySlugUrl($provinder='Ha-Noi'){

	if($provinder=='Toan-Quoc'){
		$provinder_id= 100;
	}else {
		$ci =&get_instance();
        $all_provinder = $ci->all_provinder;
        foreach($ci->all_provinder as $key=>$val){
            if($val['alias']==$provinder){
                $provinder_id= $val['id'];
            }
        }
	}

    if($ci->province_id != $provinder_id){
        $cookie = array(
            'name'=>  COOKIE_PROVINCE_ID,
            'value'=>$provinder_id,
            'expire'=>time()+86500,
            'path'=>'/'
        );
        $ci->province_id=$provinder_id;
        set_cookie($cookie);
    }
}


function getLabelProvinder($provinder_id,$label=false){
	$ci =& get_instance();
    $ci->load->model('provinder_model');
    if($provinder_id==100){
        if($label){
            return 'Toàn Quốc';
        }else {
            return 'Toan-Quoc';
        }
    }else {
        $provinder = $ci->provinder_model->getSingle($provinder_id);
        if($label==true){
            return $provinder['name'];
        }else {
            return $provinder['alias'];
        }
    }
}

function get_post_list_url($provinder_id=null, $category_id=null, $need_id=null, $type_id=null){
    $provinder =  getLabelProvinder($provinder_id);
	if(!empty($provinder_id) && !empty($category_id) && empty($need_id) && empty($type_id) ){
		$category = get_name_category($category_id);
		return site_url($provinder.'/'.$category.'-c'.$category_id,true);
	}

	if(!empty($provinder_id) && !empty($category_id) && !empty($need_id) && empty($type_id)){
		$category 	= 	get_name_category($category_id);
		$need 		=	get_name_category($need_id);
		return site_url($provinder.'/'.$need.'-'.$category.'-c'.$category_id.'-n'.$need_id,true);
	}

	if(!empty($provinder_id) && !empty($category_id) && empty($need_id) && !empty($type_id)){
		$category 	= 	get_name_category($category_id);
		$type 		=	get_name_category($type_id);
		return site_url($provinder.'/'.$type.'-'.$category.'-c'.$category_id.'-t'.$type_id,true);
	}

	if(!empty($provinder_id) && !empty($category_id) && empty($need_id) && empty($type_id) && !empty($district_id)){
		$category 	= 	get_name_category($category_id);
		return site_url($provinder.'/'.$category.'-c'.$category_id.'-d'.$district_id,true);
	}

	if(!empty($provinder_id) && !empty($category_id) && !empty($need_id) && !empty($type_id) && empty($district_id)){
		$category 	= 	get_name_category($category_id);
		$need 		=	get_name_category($need_id);
		$type 		=	get_name_category($type_id);
		return site_url($provinder.'/'.$need.'-'.$type.'-'.$category.'-c'.$category_id.'-n'.$need_id.'-t'.$type_id,true);
	}
	if(!empty($provinder_id) && !empty($category_id) && !empty($need_id) && empty($type_id) && !empty($district_id)){
		$category 	= 	get_name_category($category_id);
		$need 		=	get_name_category($need_id);
		return site_url($provinder.'/'.$need.'-'.$category.'-c'.$category_id.'-n'.$need_id.'-d'.$district_id,true);
	}
	if(!empty($provinder_id) && !empty($category_id) && !empty($need_id) && !empty($type_id) && !empty($district_id)){
		$category 	= 	get_name_category($category_id);
		$need 		=	get_name_category($need_id);
		$type 		=	get_name_category($type_id);
		return site_url($provinder.'/'.$need.'-'.$type.'-'.$category.'-c'.$category_id.'-n'.$need_id.'-t'.$type_id.'-d'.$district_id,true);
	}
}

function get_post_find_url($provinder_id=null, $category_id=null, $need_id=null, $type_id=null){

    $provinder =  getLabelProvinder($provinder_id);
    if(!empty($provinder_id) && !empty($category_id) && empty($need_id) && empty($type_id) ){
        $category = get_name_category($category_id);
        return base_url($provinder.'/'.$category.'-c'.$category_id,true);
    }

    if(!empty($provinder_id) && !empty($category_id) && !empty($need_id) && empty($type_id)){
        $category 	= 	get_name_category($category_id);
        $need 		=	get_name_category($need_id);
        return base_url($provinder.'/'.$need.'-'.$category.'-c'.$category_id.'-n'.$need_id,true);
    }

    if(!empty($provinder_id) && !empty($category_id) && empty($need_id) && !empty($type_id)){
        $category 	= 	get_name_category($category_id);
        $type 		=	get_name_category($type_id);
        return base_url($provinder.'/'.$type.'-'.$category.'-c'.$category_id.'-t'.$type_id,true);
    }
    if(!empty($provinder_id) && !empty($category_id) && !empty($need_id) && !empty($type_id)){
        $category 	= 	get_name_category($category_id);
        $need 		=	get_name_category($need_id);
        $type 		=	get_name_category($type_id);
        return base_url($provinder.'/'.$need.'-'.$type.'-'.$category.'-c'.$category_id.'-n'.$need_id.'-t'.$type_id,true);
    }
}

function get_post_detail_url($post_id=null,$title=""){
	 $link = url_title(removesign($title),true);
	 return site_url($link.'-'.$post_id,true);
}

function get_category_label($category_id,$flag = false){
    $CI =& get_instance();
    $CI->load->model('category_model');
    $category  = $CI->category_model->getSingle($category_id);
    if($flag===true){
        return  $category['name'];
    }else {
        return getTitleForUrl($category['name']);
    }
}
function get_category($cat_id){
    $ci=& get_instance();
    $data=array();
    foreach($ci->all_cats as $val){
        if($val['parent']==$cat_id){
            $data[]=$val;
        }
    }
    return $data;
}


function get_breabcrumb($provinder_id,$category_id,$sub_cat_id,$child_cat_id){
    $provinder =  getLabelProvinder($provinder_id,true);
	$link = "<span class='left'><strong>Tin đăng thuộc:</strong>&nbsp;&nbsp;</span><ul class='breabcrumb left'>";
	$link.= '<li><a href="'.site_url($provinder).'">'.$provinder.'</a></li>';

	if(!empty($provinder_id) && !empty($category_id)) {
		$link.='<li><a href="'.site_url(getLabelProvinder($provinder_id).'/'.get_name_category($category_id).'-c'.$category_id).'">'.get_category_label($category_id,true).'</a></li>';
	}
	if(!empty($provinder_id) && !empty($category_id) && !empty($sub_cat_id)) {
		$type_link = getLabelProvinder($category_id,true).'/'.get_name_category($sub_cat_id).'-'.get_name_category($category_id).'-c'.$category_id.'-n'.$sub_cat_id;
		$link.='<li><a href="'.site_url($type_link,true).'">'.get_category_label($sub_cat_id,true).'</a></li>';
	}
    if(!empty($provinder_id) && !empty($category_id) && !empty($sub_cat_id) && !empty($child_cat_id)) {
        $links = getLabelProvinder($provinder_id).'/'.get_name_category($child_cat_id).'-'.get_name_category($sub_cat_id).'-'.get_name_category($category_id).'-c'.$category_id.'-n'.$sub_cat_id.'-t'.$child_cat_id;
        $link.= '<li><a href="'.site_url($links,true).'">'.get_category_label($child_cat_id,true).'</a></li>';
    }
	$link.="</ul>";
	return $link;
}
function get_find_district($district_id){
	$ci =& get_instance();
	$query = $ci->db->query('SELECT id,name FROM district WHERE id= "'.$district_id.'" ');
	$data = $query->row_array();
	$query->free_result();
	return $data;
}
function get_name_district($district_id){
	$data = get_find_district($district_id);
	return $data['name'];
}
function get_find_category($category_id){
	$ci =& get_instance();
	$query = $ci->db->query('SELECT id,name FROM category WHERE id= "'.$category_id.'" ');
	$data = $query->row_array();
	$query->free_result();
	return $data;
}

function get_list_category(){
	$data=array();
	$ci =& get_instance();
	$query = $ci->db->query('SELECT id,name FROM category WHERE  status=1 and parent=0');
	$data = $query->result_array();
	$query->free_result();
	return $data;
}

function show_category($provinder_id){
	$CI =& get_instance();
    $data = $CI->all_cats;
	if($provinder_id==100){
		$provinder = 'Toàn Quốc';
	}else {
		$provinder = getLabelProvinder($provinder_id);
	}
	$provinder = url_title(getTitleForUrl($provinder));
	$html='<div id="ovlListCate">';
		$html.='<div class="boxOvlCityTop"><span></span></div>';
		$html.='<div class="boxOvlCity">';
				$html.='<div class="contentOvlCity">';
					$html.='<div class="RegionPanels">';
						$html.='<div class="Region" style="background: none repeat scroll 0% 0% transparent;">Danh sách Chuyên mục</div>';
						$html.='<ul>';
						foreach($data as $key=>$item){
							$category = url_title(getTitleForUrl($item['name'])).'-c'.$item['id'];
							$link = site_url($provinder.'/'.$category,true);
							$html.='<li class="City">';
								$html.='<a href="'.$link.'">'.$item['name'].'</a>';
							$html.='</li>';
						}
						$html.='</ul>';
					$html.='</div>';
				$html.='</div>';
			$html.='</div>';
		$html.='<div class="boxOvlCityBottom"></div>';
	$html.='</div>';
	return $html;
}



function get_name_category($category_id){
	$ci =& get_instance();
	$ci->load->helper(array('text'));
    $category = $ci->all_cats[$category_id];
	return getTitleForUrl($category['name']);
}

if(!function_exists("removesign")){

    function removesign($str)
    {
        $coDau=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ"
        ,"ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
            "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
        ,"ờ","ớ","ợ","ở","ỡ",
            "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
            "ỳ","ý","ỵ","ỷ","ỹ",
            "đ",
            "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
        ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
            "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
            "Ì","Í","Ị","Ỉ","Ĩ",
            "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
        ,"Ờ","Ớ","Ợ","Ở","Ỡ",
            "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
            "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
            "Đ","ê","ù","à");
        $khongDau=array("a","a","a","a","a","a","a","a","a","a","a"
        ,"a","a","a","a","a","a",
            "e","e","e","e","e","e","e","e","e","e","e",
            "i","i","i","i","i",
            "o","o","o","o","o","o","o","o","o","o","o","o"
        ,"o","o","o","o","o",
            "u","u","u","u","u","u","u","u","u","u","u",
            "y","y","y","y","y",
            "d",
            "A","A","A","A","A","A","A","A","A","A","A","A"
        ,"A","A","A","A","A",
            "E","E","E","E","E","E","E","E","E","E","E",
            "I","I","I","I","I",
            "O","O","O","O","O","O","O","O","O","O","O","O"
        ,"O","O","O","O","O",
            "U","U","U","U","U","U","U","U","U","U","U",
            "Y","Y","Y","Y","Y",
            "D","e","u","a");

		$str =	str_replace($coDau,$khongDau,$str);
		$str = str_replace(',','',$str);
		$str = str_replace('    ',' ',$str);
		$str = str_replace('   ',' ',$str);
		$str = str_replace('  ',' ',$str);
		$str = str_replace('/','',$str);
		$str = str_replace('"','',$str);
		$str = str_replace('\'','',$str);
		$str = str_replace('~','',$str);
		$str = str_replace('`','',$str);
		$str = str_replace('!','',$str);
		$str = str_replace('@','',$str);
		$str = str_replace('#','',$str);
		$str = str_replace('%','',$str);
		$str = str_replace('^','',$str);
		$str = str_replace('&','',$str);
		$str = str_replace('*','',$str);
		$str = str_replace('(','',$str);
		$str = str_replace(')','',$str);
		$str = str_replace('+','',$str);
		$str = str_replace('=','',$str);
		$str = str_replace('_','',$str);
		$str = str_replace('{','',$str);
		$str = str_replace('}','',$str);
		$str = str_replace('[','',$str);
		$str = str_replace(']','',$str);
		$str = str_replace('|','',$str);
		$str = str_replace('\\','',$str);
		$str = str_replace(':','',$str);
		$str = str_replace(';','',$str);
		$str = str_replace('?','',$str);
		$str = str_replace('>','',$str);
		$str = str_replace('<','',$str);
		$str = str_replace('.','',$str);
		$str = str_replace('----',' ',$str);
		$str = str_replace('---',' ',$str);
		$str = str_replace('--',' ',$str);
		$str = str_replace('____',' ',$str);
		$str = str_replace('___',' ',$str);
		$str = str_replace('__',' ',$str);
		$str = str_replace(';','',$str);
		$str = str_replace(':',' ',$str);
        return getTitleForUrl($str);
    }

    function getTitleForUrl($title, $romanize = false)
    {
        if ($romanize) {
            $title = utf8_romanize(utf8_deaccent($title));
        }

        $aPattern = array(
            "a" => "á|à|ạ|ả|ã|ă|ắ|ằ|ặ|ẳ|ẵ|â|ấ|ầ|ậ|ẩ|ẫ|Á|À|Ạ|Ả|Ã|Ă|Ắ|Ằ |Ặ|Ẳ|Ẵ|Â|Ấ|Ầ|Ậ|Ẩ|Ẫ",
            "o" => "ó|ò|ọ|ỏ|õ|ô|ố|ồ|ộ|ổ|ỗ|ơ|ớ|ờ|ợ|ở|ỡ|Ó|Ò|Ọ|Ỏ|Õ|Ô|Ố|Ồ |Ộ|Ổ|Ỗ|Ơ|Ớ|Ờ|Ợ|Ở|Ỡ",
            "e" => "é|è|ẹ|ẻ|ẽ|ê|ế|ề|ệ|ể|ễ|É|È|Ẹ|Ẻ|Ẽ|Ê|Ế|Ề|Ệ|Ể|Ễ",
            "u" => "ú|ù|ụ|ủ|ũ|ư|ứ|ừ|ự|ử|ữ|Ú|Ù|Ụ|Ủ|Ũ|Ư|Ứ|Ừ|Ự|Ử|Ữ",
            "i" => "í|ì|ị|ỉ|ĩ|Í|Ì|Ị|Ỉ|Ĩ",
            "y" => "ý|ỳ|ỵ|ỷ|ỹ|Ý|Ỳ|Ỵ|Ỷ|Ỹ",
            "d" => "đ|Đ",
        );
        while (list($key, $value) = each($aPattern)) {
            $title = @ereg_replace($value, $key, $title);
        }

        $title = strtr(
            $title,
            '`!"$%^&*()-+={}[]<>;:@#~,./?|' . "\r\n\t\\",
            '                             ' . '    '
        );
        $title = strtr($title, array('"' => '', "'" => ''));

        if ($romanize) {
            $title = preg_replace('/[^a-zA-Z0-9_ -]/', '', $title);
        }

        $title = preg_replace('/[ ]+/', '-', trim($title));
        $aPattern = array(
            "a" => "á|à|ạ|ả|ã|ă|ắ|ằ|ặ|ẳ|ẵ|â|ấ|ầ|ậ|ẩ|ẫ|Á|À|Ạ|Ả|Ã|Ă|Ắ|Ằ |Ặ|Ẳ|Ẵ|Â|Ấ|Ầ|Ậ|Ẩ|Ẫ",
            "o" => "ó|ò|ọ|ỏ|õ|ô|ố|ồ|ộ|ổ|ỗ|ơ|ớ|ờ|ợ|ở|ỡ|Ó|Ò|Ọ|Ỏ|Õ|Ô|Ố|Ồ |Ộ|Ổ|Ỗ|Ơ|Ớ|Ờ|Ợ|Ở|Ỡ",
            "e" => "é|è|ẹ|ẻ|ẽ|ê|ế|ề|ệ|ể|ễ|É|È|Ẹ|Ẻ|Ẽ|Ê|Ế|Ề|Ệ|Ể|Ễ",
            "u" => "ú|ù|ụ|ủ|ũ|ư|ứ|ừ|ự|ử|ữ|Ú|Ù|Ụ|Ủ|Ũ|Ư|Ứ|Ừ|Ự|Ử|Ữ",
            "i" => "í|ì|ị|ỉ|ĩ|Í|Ì|Ị|Ỉ|Ĩ",
            "y" => "ý|ỳ|ỵ|ỷ|ỹ|Ý|Ỳ|Ỵ|Ỷ|Ỹ",
            "d" => "đ|Đ",
        );
        while (list($key, $value) = each($aPattern)) {
            $title = preg_replace('/' . $value . '/i', $key, $title);
        }
        return strtr($title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
    }

	function addClass($str){
		$str = str_replace(' ','',removesign($str,true));
		$str=str_replace('-','',strtolower($str));
		return $str;
	}
    function tagsString($string){
        $search = array('@<script[^>]*?>.*?</script>@si','@<style[^>]*?>.*?</style>@siU','@<![\s\S]*?--[ \t\n\r]*>@');
        $string = preg_replace($search, '', $string);
        $string =  str_replace('div','p',$string);
        $string = strip_tags($string,'<b><span><p><br><font><strong><em><i><img><a>');
        $pattern = "/<(a)([^>]+)>/i";
        $target = "<\\1 target=\"_blank\"\\2>";
        $rel = "<\\1 rel=\"nofollow\"\\2>";
        $string = preg_replace($pattern,$target,str_replace('target="_blank"','',$string));
        $string = preg_replace($pattern,$rel,str_replace('rel="nofollow"','',$string));
        //$string = htmlentities($string);
        return  $string;
    }

        function converString($str){
        $string = removesign(trim($str));
        $strOld = strlen(str_replace(' ','',$string));
        $strNew = preg_match_all("/[A-Z0-9]/",$string,$matches);
        if($strOld==count($matches[0])){
            return ucfirst(mb_strtolower($str,'UTF-8'));
        }else {
            return ucfirst($str);
        }
    }
}

function is_member(){
    $ci =& get_instance();
    $userdata = $ci->session->userdata('member');
    if(!empty($userdata)){
        return true;
    }
    return false;
}
function getUser(){
    $ci =& get_instance();
    $userdata = $ci->session->userdata('member');
    return  $userdata;
}
function getByUser($user_id,$flag=0){
    $ci = &get_instance();
    $ci->load->model('user_model');
    $data = $ci->user_model->getSingle($user_id);
    if($flag==1){
        return  $data['email'];
    }else if($flag==2){
        return $data['username'];
    }
    else if($flag===3){
            return $data['avatar'];
    }else {
        return  $data;
    }
}

function set_global_messages($msg){
    return $msg;
}

function getByShop($user_id){
    $ci = & get_instance();
    $ci->load->model('user_model');
    $getByUser = $ci->user_model->getSingle($user_id);
    $link =  '<a href="'.base_url('shop/'.$getByUser['username']).'">'.$getByUser['username'].'</a>';
    return $link;
}

function getRemainingTime($endTime) {
    $currentTime = strtotime(date('d-m-Y'));
    if($currentTime>$endTime){
        return  date('H:m:s | d-m-Y',$endTime);
    }else {
        return  date('H:m:s',$endTime).' | Hôm nay';
    }
}

function getTygia(){
    $link='http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx';
    $xml = file_get_contents($link);
    $xmlData = NULL;
    $p = xml_parser_create();
    xml_parse_into_struct($p,$xml , $xmlData);
    xml_parser_free($p);
    var_dump($xml);die;
}